<template>
  <scroll-view :scroll-y="true" class="h-11/12 m-auto w-11/12">
    <uni-forms :modelValue="formData">
      <!-- 菜肴类型 -->
      <uni-card title="菜肴类型" extra="必填*">
        <uni-forms-item>
          <uni-data-checkbox
            v-model="formData.caiYaoLeiXing"
            :localdata="caiYaoLeiXing"
            :disabled="isEdit"
          />
        </uni-forms-item>
      </uni-card>
      <!-- 餐次时间 -->
      <uni-card title="餐次时间" extra="必填*">
        <uni-forms-item>
          <uni-data-checkbox
            v-model="formData.canCiShiJian"
            :localdata="canCiShiJian"
            :disabled="isEdit"
          />
        </uni-forms-item>
        <!-- 宴会名称 -->
        <uni-forms-item
          v-if="formData.canCiShiJian === '宴会(可在下个题项备注名称)'"
        >
          宴会名称:
          <uni-easyinput
            type="textarea"
            v-model="formData.yanHuiMingCheng"
            :disabled="isEdit"
          />
        </uni-forms-item>
      </uni-card>
      <!-- 食品名称 -->
      <uni-card title="食品名称" extra="必填*">
        <uni-forms-item>
          <uni-easyinput
            type="textarea"
            v-model="formData.shiPinMingCheng"
            :disabled="isEdit"
          />
        </uni-forms-item>
      </uni-card>
      <!-- 食品制备人 -->
      <uni-card title="食品制备人" extra="必填*">
        <uni-forms-item>
          <uni-easyinput
            v-model="formData.shiPinZhiBeiRen"
            :disabled="isEdit"
          />
        </uni-forms-item>
      </uni-card>
      <!-- 留样储存温度(℃) -->
      <uni-card title="留样储存温度(℃)" extra="必填*">
        <uni-forms-item>
          <uni-easyinput
            v-model="formData.liuYangChuCunWenDu"
            :disabled="isEdit"
          />
        </uni-forms-item>
      </uni-card>
      <!-- 留样人 -->
      <uni-card title="留样人" extra="必填*">
        <uni-forms-item>
          <uni-easyinput v-model="formData.liuYangRen" :disabled="isEdit" />
        </uni-forms-item>
      </uni-card>
      <uni-card title="评语" v-show="props.id">
        <uni-easyinput v-model="reviewOpinion" />
      </uni-card>
    </uni-forms>

    <button
      @click="submitForm"
      class="mb-10 w-2/3 rounded-xl"
      style="background-color: #5199ff"
    >
      {{ submitBtnName }}
    </button>
    <ShowConfigComponent
      name="shiPinLiuYang"
      @showFormData="showFormData"
      v-show="!isEdit"
    />
  </scroll-view>
</template>

<script lang="ts" setup>
import { submitMenuIdList } from '@/services/review';
import type { RequestMenuIdList } from '@/services/types/review';
import type {
  FormDetail,
  returnSubmitData,
  submitTableData,
  tableDate,
} from '@/services/types/write';
import { createTableById, showTableById, submitTable } from '@/services/write';
import {
  splitName,
  transformData,
  compareLoadData,
  random0_4,
  random0_09,
  randomPlusOrMinus,
} from '@/utils/utils';
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';

// 表格提交信息
const formData = ref({
  caiYaoLeiXing: '', //菜肴类型
  canCiShiJian: '', //餐次时间
  yanHuiMingCheng: '', //宴会名称
  shiPinMingCheng: '', //食品名称
  shiPinZhiBeiRen: '', //食品制备人
  liuYangChuCunWenDu: '', //留样储存温度(℃)
  liuYangRen: '', // 留样人
});
// 菜肴类型数据
const caiYaoLeiXing = [
  {
    text: '冷食',
    value: '冷食',
  },
  {
    text: '热食',
    value: '热食',
  },
  {
    text: '点心/甜品',
    value: '点心/甜品',
  },
  {
    text: '客人自带',
    value: '客人自带',
  },
  {
    text: '饮品',
    value: '饮品',
  },
  {
    text: '其他',
    value: '其他',
  },
];
// 餐次时间
const canCiShiJian = [
  {
    text: '早餐',
    value: '早餐',
  },
  {
    text: '午餐',
    value: '午餐',
  },
  {
    text: '晚餐',
    value: '晚餐',
  },
  {
    text: '宵夜',
    value: '宵夜',
  },
  {
    text: '茶歇',
    value: '茶歇',
  },
  {
    text: '宴会(可在下个题项备注名称)',
    value: '宴会(可在下个题项备注名称)',
  },
];
// 是否可编辑
const isEdit = ref(false);
// 提交按钮名字
const submitBtnName = ref('');
const props = defineProps({
  value: String,
  id: String,
});
// 评语
const reviewOpinion = ref('');
/**
 * 表格相关数据
 */
const table = () => {
  const tableData = ref<tableDate[]>([]);
  const reportingFormOperateId = ref('');
  // 如果提交按钮等于提交则创建表格
  const createTable = async () => {
    submitBtnName.value = '提交';
    formData.value.liuYangChuCunWenDu = '';
    formData.value.liuYangChuCunWenDu =
      randomPlusOrMinus(random0_4() - 1) + random0_09() + '°C';
    // 创建表格
    uni.showLoading({
      title: '加载中...',
    });
    const res = await createTableById('8286263942992691200');
    reportingFormOperateId.value = res.reportingFormOperateId;
    tableData.value = res.data;
    uni.hideLoading();
  };
  // 提交表格
  const submitNewTable = (
    reportingFormOperateId: string,
    tableData: tableDate[],
  ): submitTableData => {
    const formDetail = transformData(tableData) as FormDetail[];
    //菜肴类型
    formDetail[0].componentValue = formData.value.caiYaoLeiXing;
    formDetail[0].options.filter((item) => {
      if (splitName(item.optionTitle) === formData.value.caiYaoLeiXing) {
        item.selected = true;
      }
    });
    //餐次时间
    formDetail[1].componentValue = formData.value.canCiShiJian;
    formDetail[1].options.filter((item) => {
      if (splitName(item.optionTitle) === formData.value.canCiShiJian) {
        item.selected = true;
      }
    });
    //宴会名称
    formDetail[2].componentValue = formData.value.yanHuiMingCheng;
    //食品名称
    formDetail[3].componentValue = formData.value.shiPinMingCheng;
    //食品制备人
    formDetail[4].componentValue = formData.value.shiPinZhiBeiRen;
    //留样储存温度(℃)
    formDetail[5].componentValue = formData.value.liuYangChuCunWenDu;
    // 留样人
    formDetail[6].componentValue = formData.value.liuYangRen;
    // 其他数据
    const submitTable = ref<submitTableData>({
      formDetail: formDetail,
      reportingFormOperateId: reportingFormOperateId,
      status: 1,
      unionMenuId: '8286263942992691200',
    });

    return submitTable.value;
  };
  return {
    createTable,
    tableData,
    reportingFormOperateId,
    submitNewTable,
  };
};
const { createTable, submitNewTable, tableData, reportingFormOperateId } =
  table();
// 根据id展示数据
const showDataById = async (id: string) => {
  uni.showLoading({
    title: '加载中...',
  });
  const res = await showTableById(id);
  if (res.msg === 'success') {
    //菜肴类型
    res.data.formDetail[0].options.filter((item) => {
      if (item.selected) {
        formData.value.caiYaoLeiXing = splitName(item.optionTitle);
      }
    });
    //餐次时间
    res.data.formDetail[1].options.filter((item) => {
      if (item.selected) {
        formData.value.canCiShiJian = splitName(item.optionTitle);
      }
    });
    //宴会名称
    formData.value.yanHuiMingCheng = res.data.formDetail[2].componentValue;

    //食品名称
    formData.value.shiPinMingCheng = res.data.formDetail[3].componentValue;

    //食品制备人
    formData.value.shiPinZhiBeiRen = res.data.formDetail[4].componentValue;

    //留样储存温度(℃)
    formData.value.liuYangChuCunWenDu = res.data.formDetail[5].componentValue;
    // 留样人
    formData.value.liuYangRen = res.data.formDetail[6].componentValue;
    // 评语
    reviewOpinion.value = (
      res.data as returnSubmitData
    ).handleOption.reviewOpinion;
  }
  uni.hideLoading();
};
onLoad(async () => {
  // 判断提交按钮是否等于newTable，
  // 等于newTable则执行创建表格
  if (props.value === 'newTable') {
    createTable();
  } else if (props.id) {
    showDataById(props.id);
    submitBtnName.value = '审批';
    isEdit.value = true;
  }
});
// 提交
const submitForm = () => {
  // 等于newTable则执行提交表格
  if (props.value === 'newTable') {
    uni.showModal({
      title: '确认提交？',
      content: '提交后将无法修改，请确认是否提交',
      success: function (res) {
        if (res.confirm) {
          submitTable(
            submitNewTable(reportingFormOperateId.value, tableData.value),
          ).then((res) => {
            if (res.msg === 'success') {
              uni.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 500,
              });
              setTimeout(() => {
                uni.navigateBack({
                  delta: 1,
                });
              }, 500);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      },
    });
  } else if (props.id) {
    // 有id意味查看详情
    const data = ref<RequestMenuIdList>({
      id: props.id,
    });
    uni.showModal({
      title: '确认提交？',
      content: '提交后将无法修改，请确认是否提交',
      success: function (res) {
        if (res.confirm) {
          submitMenuIdList(data.value, reviewOpinion.value).then((res) => {
            if (res.data === 'success') {
              uni.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 500,
              });
              setTimeout(() => {
                uni.navigateBack({
                  delta: 1,
                });
              }, 500);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      },
    });
  }
};
// 加载配置
const showFormData = (data: any) => {
  compareLoadData(formData.value, data);
};
</script>

<style lang="scss" scoped>
::v-deep.uni-card {
  .uni-card__header {
    .uni-card__header-extra {
      .uni-card__header-extra-text {
        color: #ff0000;
      }
    }
  }
}
</style>
